package cn.com.bluemoon.daps.system.client;

import cn.com.bluemoon.datasecurity.app.sdk.RemoteMetaDataColumnAuthService;
import cn.com.bluemoon.datasecurity.app.sdk.RemoteMetaDataTableExtService;
import cn.com.bluemoon.metadata.common.ResultBean;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Jarod.Kong
 */
@Component
@Slf4j
public class DapDataSecurityModuleFacade {
    @Resource
    private RemoteMetaDataColumnAuthService remoteMetaDataColumnAuthService;

    @Resource
    private RemoteMetaDataTableExtService remoteMetaDataTableExtService;

    /**
     * 集成数据安全中需要刷新业务线信息
     */
    public void refreshOnUpdateBizLine(Integer bizLineId, String admins, String dataManager) {
        try {
            List<String> adminMgs = new ArrayList<>();
            adminMgs.add(dataManager);
            if (admins != null) {
                String[] adminManagers = admins.split(",");
                adminMgs.addAll(Arrays.asList(adminManagers));
            }
            @SuppressWarnings("rawtypes") ResultBean res = remoteMetaDataColumnAuthService.refreshBizLineAdminAuth(bizLineId, adminMgs);
            log.info("刷新数据安全模块业务线信完成，情况：{}", res);
        } catch (Exception e) {
            log.error("刷新数据安全模块业务线信息失败, bizLineId={}", bizLineId, e);
        }
    }

    public BizLineMetadataRel relMetaDataTableOnDeleteBizLine(Integer bizLineId) {
        ResultBean<Boolean> res = null;
        try {
            // false
            res = remoteMetaDataTableExtService.isBizLineRelationMetaDataTable(bizLineId);
        } catch (Exception e) {
            log.error("服务调用数据安全-业务线ID下是否有关联的元数据表接口失败，bizLineId={}", bizLineId, e);
            return new BizLineMetadataRel(false, "服务调用数据安全-业务线ID下是否有关联的元数据表接口失败");
        }
        Boolean hadRelation = res.getContent();
        return new BizLineMetadataRel(!hadRelation, res.getMsg());
    }


    @AllArgsConstructor
    @Getter
    public static class BizLineMetadataRel {
        private final boolean canDelete;

        /**
         * canDelete为false时候，错误信息
         */
        private final String warnMsg;
    }
}

